// 获取验证码
const getCode = {
	data() {
		return {
			codeClick: true,      // 是否可以点击获取验证码
			codeText: '验证码',    // 获取验证码文字
			codeInterval: null    // 验证码倒计时计时器
		}
	},
	onUnload() {
		clearInterval(this.codeInterval);
	},
	methods: {
		// 获取短信验证码
		getCode(phone,type) {
		  if (!this.codeClick) return;
			
		  this.codeClick = false;
		  this.codeText = '获取中';
			console.log(phone)
		  this.request.post('auth-code',{
			  type,//1-注册2-忘记密码
			  phone:phone,//手机号
		  })
				.then(res => {
					if (res.data.code === 1) {
						let i = 60;
						this.codeInterval = setInterval(() => {
							i --;
							this.codeText = i + 's后获取';
							if (i <= 0) this.resetCode();
						}, 1000);
					} else this.resetCode();
				})
				.catch(err => {
					this.resetCode();
				});
		},
		// 重置验证码
		resetCode() {
			clearInterval(this.codeInterval);
			this.codeText = '重新获取';
			this.codeClick = true;
		},
		restoreCode() {
			clearInterval(this.codeInterval);
			this.codeText = '验证码';
			this.codeClick = true;
		},
	}
};

// 页面滚动更改状态栏样式
const navScroll = {
	data() {
		return {
			navbarBg: '',        // 导航栏背景色
			navbarWhite: true    // 导航是否白色文字
		}
	},
	onPageScroll(e) {
		// 监听页面滚动修改导航栏样式
		let scroll = e.scrollTop / 100;
		if (scroll >= 1) scroll = 1;
		this.navbarWhite = scroll < 0.5;
		this.navbarBg = `rgba(255, 255, 255, ${scroll})`;
	}
};

// 底部导航栏的页面滚动更改状态栏样式
const tabScroll = {
	data() {
		return {
			scroll: 0    // 滚动的距离
		}
	},
	onPageScroll(e) {
		this.scroll = e.scrollTop;
	},
};

// 分页
const paging = {
	data() {
		return {
			paging: {
				load: true,       // 是否处于加载状态
				finish: false,    // 是否加载完成
				limit: 15,        // 每页条数
				page: 1           // 页码
			}
		}
	},
	methods: {
		// 更改页码
		changePage(list) {
			this.paging.finish = list.length < this.paging.limit;
			this.paging.page ++;
		}
	}
};

// 轮播切换
const tabSwiper = {
	data() {
		return {
			current: 0,           // 轮播序号
		}
	},
	methods: {
		// 轮播切换
		swiperChange(e) {
			this.current = e.detail.current;
		}
	}
};

export {
	getCode,
	navScroll,
	tabScroll,
	paging,
	tabSwiper
};